export default Polygon
import {c, mouse} from "./canvas";
import {generatePolygon} from "./convexHull";
import {dcmp, getNormalVec, Line, Point, PointSort, projection, seg_cross_seg} from "./PlanimetryUtils";


/*
* x: 左上角的x坐标
* y: 左上角的y坐标
* weight: 区域宽度
* height: 区域高度
* color: 颜色
*/
function Polygon(x, y, weight, height, color = 'white', state = 'default') {
    this.x = x;
    this.y = y;
    this.weight = weight;
    this.height = height;
    this.color = color;
    this.state = state;

    this.pointsInfo = generatePolygon(x, y, weight, height, this.state);

    this.points = this.pointsInfo[0];
    this.n = this.pointsInfo[1];

    for (let i = 0; i < this.n; ++i) {
        this.points[i][0] = this.points[i][0] - this.x;
        this.points[i][1] = this.points[i][1] - this.y;
    }

    this.show = function () {
        c.beginPath();
        for (let i = 0; i < this.n; ++i) {
            c.lineTo(this.points[i][0] + this.x,
                this.points[i][1] + this.y);
        }
        c.closePath();
        c.fillStyle = this.color;
        c.fill();
        c.stroke();
    }

    this.update = function () {
        this.show();
    }
}

function max(a, b) {
    return a > b ? a : b;
}

function min(a, b) {
    return a < b ? a : b;
}

// a: Polygon
// b: Polygon
// return true: collision detected
// return false: no collision detected
function Detect(a, b) {
    let aPoints = [], bPoints = [];
    for (let i = 0; i < a.n; ++i) {
        aPoints[i] = new Point([a.points[i][0] + a.x, a.points[i][1] + a.y]);
    }
    for (let i = 0; i < b.n; ++i) {
        bPoints[i] = new Point([b.points[i][0] + b.x, b.points[i][1] + b.y]);
    }
    aPoints[a.n] = aPoints[0];
    bPoints[b.n] = bPoints[0];
    // console.log(aPoints)
    // console.log(bPoints)

    let tmpPointsA = [], tmpPointsB = [];

    for (let i = 0; i < a.n; ++i) {
        // line:当前边 normalVec:法向量
        let line = new Line(aPoints[i], aPoints[i + 1]);
        let normalVec = new Line(new Point([0, 0]), getNormalVec(line));
        console.log(normalVec)
        // 获取a的顶点在法向量上的投影点
        for (let j = 0; j < a.n; ++j) {
            tmpPointsA[j] = projection(new Point([a.points[j][0] + a.x, a.points[j][1] + a.y]), normalVec);
        }
        PointSort(tmpPointsA);

        // 获取b的顶点在法向量上的投影点
        for (let j = 0; j < b.n; ++j) {
            tmpPointsB[j] = projection(new Point([b.points[j][0] + b.x, b.points[j][1] + b.y]), normalVec);
        }
        PointSort(tmpPointsB);

        // console.log(tmpPointsA)
        // console.log(tmpPointsB)
        // console.log(lineAProjection)
        // console.log(lineBProjection)


        // 获取a和b在法向量上的投影线段
        let lineAProjection = new Line(tmpPointsA[0], tmpPointsA[a.n - 1]);
        let lineBProjection = new Line(tmpPointsB[0], tmpPointsB[b.n - 1]);

        if (dcmp(normalVec.e.x) === 0) {
            if (max(lineAProjection.s.y, lineBProjection.s.y) >
                min(lineAProjection.e.y, lineBProjection.e.y)) {
                console.log(max(lineAProjection.s.y, lineBProjection.s.y),
                    min(lineAProjection.e.y, lineBProjection.e.y))
                console.log(tmpPointsA)
                console.log(tmpPointsB)
                console.log(lineAProjection)
                console.log(lineBProjection)
                return false;
            }

        } else {
            if (max(lineAProjection.s.x, lineBProjection.s.x) >
                min(lineAProjection.e.x, lineBProjection.e.x)) {
                console.log(max(lineAProjection.s.x, lineBProjection.s.x),
                    min(lineAProjection.e.x, lineBProjection.e.x))
                console.log(tmpPointsA)
                console.log(tmpPointsB)
                console.log(lineAProjection)
                console.log(lineBProjection)
                return false;
            }

        }

    }
    return true;
}

function CollisionDetect(a, b) {
    return Detect(a, b) && Detect(b, a);
}

export {CollisionDetect}
